from multiprocessing import Process
import time,os
# ________________进程创建________________________
# def Hi(name):
#     time.sleep(3)
#     print("Hello %s" % name,time.ctime())
#     print("进程号是 ",os.getpid())
# if __name__=="__main__":
#     for i in range(3):
#         t = Process(target=Hi,args=("萌萌",))
#         t.start()
#     print("主进程号：",os.getpid())
#     print("end  all")
# ______________进程阻碍join______________________
# def hello(name):
#     time.sleep(2)
#     print("Hello %s" % name,time.ctime())
#     print("进程号是 ",os.getpid())
# if __name__=="__main__":
#     L=[]
#     for i in range(3):
#         t= Process(target=hello,args=("梦梦",))
#         t.start()
#         L.append(t)
#     # t.join()
#     # t.join() # 阻碍主进程
#     for t in L:
#         t.join()
#     print("主进程号：",os.getpid())
#     print("end  all")



# ---------------继承调用方式调用方式-------------------
class MyProcess(Process):
    # def __init__(self,name):
    #     super(MyProcess,self).__init__()
    #     self.name =name
    def run(self):
        time.sleep(1)
        print("hello",self.name,time.ctime())

if __name__ == '__main__':
    p_list=[]
    for i in range(3):
        # p =MyProcess("张三")
        p = MyProcess()
        p.start()
        p_list.append(p)
    # p.join()
    for p in p_list:
        p.join()
    print("end")

# ___''Daemon:进程守护、子进程跟随主进程一起退'''_______

class myprocess:
    def music1(self):
        print("begin to listen %s" %time.ctime())
        time.sleep(2)
        print("stop to listen %s" % time.ctime())
    def game1(self):
        print("begin to game %s" % time.ctime())
        time.sleep(3)
        print("stop to game %s" % time.ctime())

if __name__ == "__main__":
    duixiang=myprocess()
    p1 = Process(target=duixiang.music1)
    p2 = Process(target=duixiang.game1)
    p2.daemon=True
    L=[]
    L.append(p1)
    L.append(p2)
    for i in L:
        i.start()
    # i.join()

    print("all over end.......")
# __________________Process类下的方法___________________
''' 
    1.实例方法
    p.is_alive() # 返回bloon值 进程是否在运行
    p.terminate() # 不管任务是否完成、立即停止工作进程
    p.start()
    p.run()
    p.join()
    2.属性方法
    daemon、pid
#     '''
# import os
# def info(title):
#     print("title",title)
#     print("父进程",os.getppid())
#     print("进程号",os.getpid())
# def foo(name):
#     info("function foo")
#     print("hello ",name)
#
# if __name__ == '__main__':
#     info("main process line")
#     time.sleep(1)
#     print("____________")
#     p =Process(target=info,args=("小高",))
#     p.start()
#     print(p.is_alive())
#     p.join()
